﻿using System;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Revit;

namespace YArchitech.Temp
{
	internal static class RevitDataExchange
	{
		public static void ToPipeSpecification(Connector conn, ref HYPipeSpecification ds)
		{
			if (conn == null)
			{
				return;
			}
			ds.Diameter = (double)((int)RevitUtils.Fix(RevitUtils.DistanceToInternal(2.0 * conn.Radius) * 1000.0, 3));
			ds.Tag = conn;
		}

		public static HYPipeFitting CreateFitting(FamilyInstance fi, Connector connIn, Connector connOut, Connector connSub1, Connector connSub2)
		{
			MechanicalFitting mechanicalFitting = fi.MEPModel as MechanicalFitting;
			if (mechanicalFitting == null)
			{
				return null;
			}
			double value = RevitUtils.GetValue(fi, BuiltInParameter.RBS_LOSS_COEFFICIENT);
			string familyInstanceName = RevitUtils.GetFamilyInstanceName(fi);
			HYPipeFitting hypipeFitting;
            switch ((int)mechanicalFitting.PartType)
			{
			case 5:
				hypipeFitting = RevitDataExchange.CreateElbow(fi, connIn, connOut);
				break;
			case 6:
				hypipeFitting = RevitDataExchange.CreateTee(fi, connIn, connOut, connSub1);
				break;
			case 7:
				hypipeFitting = RevitDataExchange.CreateTransition(fi, connIn, connOut);
				break;
			case 8:
				hypipeFitting = RevitDataExchange.CreateCross(fi, connIn, connOut, connSub1, connSub2);
				break;
			default:
				hypipeFitting = new HYPipeFitting();
				break;
			}
			if (hypipeFitting != null)
			{
				hypipeFitting.Id = fi.Id.IntegerValue;
				hypipeFitting.Coefficient = value;
				hypipeFitting.Name = familyInstanceName;
			}
			return hypipeFitting;
		}

		public static HYPipeStraight CreateStraight(Pipe revitPipe)
		{
			HYPipeStraight hypipeStraight = new HYPipeStraight();
			hypipeStraight.Id = revitPipe.Id.IntegerValue;
			HYPipeSpecification section = new HYPipeSpecification();
			RevitDataExchange.ToPipeSpecification(RevitUtils.GetMEPObjectConnector(revitPipe), ref section);
			hypipeStraight.Section = section;
			hypipeStraight.Number = "RS_" + RevitUtils.GetRevitNumber(revitPipe);
			hypipeStraight.Roughness = RevitUtils.GetRoughness(revitPipe, BuiltInParameter.RBS_PIPE_ROUGHNESS_PARAM);
			hypipeStraight.Length = RevitUtils.GetCurveLength(revitPipe, BuiltInParameter.CURVE_ELEM_LENGTH);
			hypipeStraight.Velocity = RevitUtils.GetVelocity(revitPipe, BuiltInParameter.RBS_PIPE_VELOCITY_PARAM);
			hypipeStraight.AddFlow = RevitUtils.GetFlow(revitPipe, BuiltInParameter.RBS_PIPE_ADDITIONAL_FLOW_PARAM);
			hypipeStraight.Rm = RevitUtils.GetRm(revitPipe, BuiltInParameter.RBS_PIPE_FRICTION_PARAM);
			hypipeStraight.FrictionalResistance = RevitUtils.GetPressure(revitPipe, BuiltInParameter.RBS_PIPE_PRESSUREDROP_PARAM);
			hypipeStraight.Resistance = hypipeStraight.ShockResistance + hypipeStraight.FrictionalResistance;
			hypipeStraight.Re = RevitUtils.GetValue(revitPipe, BuiltInParameter.RBS_PIPE_REYNOLDS_NUMBER_PARAM);
			return hypipeStraight;
		}

		public static HYPipeStraight CreateStraight(FlexPipe revitPipe)
		{
			HYPipeStraight hypipeStraight = new HYPipeStraight();
			hypipeStraight.Id = revitPipe.Id.IntegerValue;
			HYPipeSpecification section = new HYPipeSpecification();
			RevitDataExchange.ToPipeSpecification(RevitUtils.GetMEPObjectConnector(revitPipe), ref section);
			hypipeStraight.Section = section;
			hypipeStraight.Number = "RF_" + RevitUtils.GetRevitNumber(revitPipe);
			hypipeStraight.Roughness = RevitUtils.GetRoughness(revitPipe, BuiltInParameter.RBS_PIPE_ROUGHNESS_PARAM);
			hypipeStraight.Length = RevitUtils.GetCurveLength(revitPipe, BuiltInParameter.CURVE_ELEM_LENGTH);
			hypipeStraight.Velocity = RevitUtils.GetVelocity(revitPipe, BuiltInParameter.RBS_PIPE_VELOCITY_PARAM);
			hypipeStraight.AddFlow = RevitUtils.GetFlow(revitPipe, BuiltInParameter.RBS_PIPE_ADDITIONAL_FLOW_PARAM);
			hypipeStraight.Rm = RevitUtils.GetRm(revitPipe, BuiltInParameter.RBS_PIPE_FRICTION_PARAM);
			hypipeStraight.FrictionalResistance = RevitUtils.GetPressure(revitPipe, BuiltInParameter.RBS_PIPE_PRESSUREDROP_PARAM);
			hypipeStraight.Resistance = hypipeStraight.ShockResistance + hypipeStraight.FrictionalResistance;
			hypipeStraight.Re = RevitUtils.GetValue(revitPipe, BuiltInParameter.RBS_PIPE_REYNOLDS_NUMBER_PARAM);
			return hypipeStraight;
		}

		public static HYPipeAccessory CreateAccessory(FamilyInstance fi, Connector connIn)
		{
			HYPipeAccessory hypipeAccessory = new HYPipeAccessory();
			hypipeAccessory.Id = fi.Id.IntegerValue;
			hypipeAccessory.ShockResistance = RevitUtils.GetPressure(fi, BuiltInParameter.RBS_PIPE_PRESSUREDROP_PARAM);
			hypipeAccessory.Name = RevitUtils.GetFamilyInstanceName(fi);
			HYPipeSpecification section = new HYPipeSpecification();
			RevitDataExchange.ToPipeSpecification(connIn, ref section);
			hypipeAccessory.Section = section;
			return hypipeAccessory;
		}

		public static HYPipeElbow CreateElbow(FamilyInstance fi, Connector connIn, Connector connOut)
		{
			HYPipeElbow hypipeElbow = new HYPipeElbow();
			hypipeElbow.Id = fi.Id.IntegerValue;
			hypipeElbow.Coefficient = RevitUtils.GetFamilyCoefficient(fi);
			hypipeElbow.Name = RevitUtils.GetFamilyInstanceName(fi);
			hypipeElbow.Angle = RevitUtils.GetAngle(fi);
			HYPipeSpecification inPort = new HYPipeSpecification();
			RevitDataExchange.ToPipeSpecification(connIn, ref inPort);
			hypipeElbow.InPort = inPort;
			HYPipeSpecification outPort = new HYPipeSpecification();
			RevitDataExchange.ToPipeSpecification(connIn, ref outPort);
			hypipeElbow.OutPort = outPort;
			return hypipeElbow;
		}

		public static HYPipeTransition CreateTransition(FamilyInstance fi, Connector connIn, Connector connOut)
		{
			HYPipeTransition hypipeTransition = new HYPipeTransition();
			hypipeTransition.Id = fi.Id.IntegerValue;
			hypipeTransition.Coefficient = RevitUtils.GetFamilyCoefficient(fi);
			hypipeTransition.Name = RevitUtils.GetFamilyInstanceName(fi);
			HYPipeSpecification inPort = new HYPipeSpecification();
			RevitDataExchange.ToPipeSpecification(connIn, ref inPort);
			hypipeTransition.InPort = inPort;
			HYPipeSpecification outPort = new HYPipeSpecification();
			RevitDataExchange.ToPipeSpecification(connIn, ref outPort);
			hypipeTransition.OutPort = outPort;
			return hypipeTransition;
		}

		public static HYPipeTee CreateTee(FamilyInstance fi, Connector teeIn, Connector teeOut, Connector teeSide)
		{
			HYPipeTee hypipeTee = new HYPipeTee();
			hypipeTee.Coefficient = RevitUtils.GetFamilyCoefficient(fi);
			hypipeTee.Name = RevitUtils.GetFamilyInstanceName(fi);
			RevitDataExchange.GetTeeLinkInfo(hypipeTee, teeIn, teeOut, teeSide);
			return hypipeTee;
		}

		public static HYPipeCross CreateCross(FamilyInstance fi, Connector connIn, Connector connOut, Connector connSub1, Connector connSub2)
		{
			HYPipeCross hypipeCross = new HYPipeCross();
			RevitDataExchange.GetTeeLinkInfo(hypipeCross, connIn, connOut, connSub1);
			HYPipeSpecification side2Port = new HYPipeSpecification();
			RevitDataExchange.ToPipeSpecification(connSub2, ref side2Port);
			hypipeCross.Side2Port = side2Port;
			hypipeCross.Side2Flow = RevitUtils.FlowToInternal(connSub2.Flow);
			hypipeCross.Side2Angle = RevitUtils.GetConnectorAngle(connOut, connSub2);
			return hypipeCross;
		}

		public static void GetTeeLinkInfo(HYPipeTee tee, Connector teeIn, Connector teeOut, Connector side)
		{
			HYPipeSpecification inPort = new HYPipeSpecification();
			RevitDataExchange.ToPipeSpecification(teeIn, ref inPort);
			HYPipeSpecification outPort = new HYPipeSpecification();
			RevitDataExchange.ToPipeSpecification(teeOut, ref outPort);
			HYPipeSpecification sidePort = new HYPipeSpecification();
			RevitDataExchange.ToPipeSpecification(side, ref sidePort);
			tee.InPort = inPort;
			tee.Flow = RevitUtils.FlowToInternal(teeIn.Flow);
			tee.OutPort = outPort;
			tee.OutFlow = RevitUtils.FlowToInternal(teeOut.Flow);
			tee.SidePort = sidePort;
			tee.SideFlow = RevitUtils.FlowToInternal(side.Flow);
			tee.Angle = Math.PI - RevitUtils.GetConnectorAngle(teeIn, teeOut);
			tee.SideAngle = RevitUtils.GetConnectorAngle(teeOut, side);
		}

		public static HYPipeTerminal CreateTerminal(FamilyInstance fi, Connector conn)
		{
			if (fi == null)
			{
				return null;
			}
			HYPipeTerminal hypipeTerminal = new HYPipeTerminal();
			hypipeTerminal.Id = fi.Id.IntegerValue;
			hypipeTerminal.Name = RevitUtils.GetFamilyInstanceName(fi);
			HYPipeSpecification port = new HYPipeSpecification();
			RevitDataExchange.ToPipeSpecification(conn, ref port);
			hypipeTerminal.Port = port;
			hypipeTerminal.Flow = RevitUtils.FlowToInternal(conn.Flow);
			hypipeTerminal.PressureDrop = RevitUtils.PressureToInternal(conn.PressureDrop);
			hypipeTerminal.Resistance = hypipeTerminal.PressureDrop;
			hypipeTerminal.DynamicPressure = RevitUtils.PressureToInternal(conn.VelocityPressure);
			Parameter parameter = fi.GetParameter("阻力损失");
			if (parameter == null)
			{
				parameter = fi.GetParameter("水头损失");
			}
			if (parameter != null)
			{
				hypipeTerminal.Resistance = RevitUtils.PressureToInternal(parameter.AsDouble());
			}
			return hypipeTerminal;
		}
	}
}
